{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "<table class=\"ee-notebook-buttons\" align=\"left\">\n",
        "    <td><a target=\"_blank\"  href=\"https://github.com/giswqs/earthengine-py-notebooks/tree/master/JavaScripts/Demos/Classification.ipynb\"><img width=32px src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" /> View source on GitHub</a></td>\n",
        "    <td><a target=\"_blank\"  href=\"https://nbviewer.jupyter.org/github/giswqs/earthengine-py-notebooks/blob/master/JavaScripts/Demos/Classification.ipynb\"><img width=26px src=\"https://upload.wikimedia.org/wikipedia/commons/thumb/3/38/Jupyter_logo.svg/883px-Jupyter_logo.svg.png\" />Notebook Viewer</a></td>\n",
        "    <td><a target=\"_blank\"  href=\"https://colab.research.google.com/github/giswqs/earthengine-py-notebooks/blob/master/JavaScripts/Demos/Classification.ipynb\"><img src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" /> Run in Google Colab</a></td>\n",
        "</table>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Install Earth Engine API and geemap\n",
        "Install the [Earth Engine Python API](https://developers.google.com/earth-engine/python_install) and [geemap](https://github.com/giswqs/geemap). The **geemap** Python package is built upon the [ipyleaflet](https://github.com/jupyter-widgets/ipyleaflet) and [folium](https://github.com/python-visualization/folium) packages and implements several methods for interacting with Earth Engine data layers, such as `Map.addLayer()`, `Map.setCenter()`, and `Map.centerObject()`.\n",
        "The following script checks if the geemap package has been installed. If not, it will install geemap, which automatically installs its [dependencies](https://github.com/giswqs/geemap#dependencies), including earthengine-api, folium, and ipyleaflet.\n",
        "\n",
        "**Important note**: A key difference between folium and ipyleaflet is that ipyleaflet is built upon ipywidgets and allows bidirectional communication between the front-end and the backend enabling the use of the map to capture user input, while folium is meant for displaying static data only ([source](https://blog.jupyter.org/interactive-gis-in-jupyter-with-ipyleaflet-52f9657fa7a)). Note that [Google Colab](https://colab.research.google.com/) currently does not support ipyleaflet ([source](https://github.com/googlecolab/colabtools/issues/60#issuecomment-596225619)). Therefore, if you are using geemap with Google Colab, you should use [`import geemap.eefolium`](https://github.com/giswqs/geemap/blob/master/geemap/eefolium.py). If you are using geemap with [binder](https://mybinder.org/) or a local Jupyter notebook server, you can use [`import geemap`](https://github.com/giswqs/geemap/blob/master/geemap/geemap.py), which provides more functionalities for capturing user input (e.g., mouse-clicking and moving)."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": [
        "# Installs geemap package\n",
        "import subprocess\n",
        "\n",
        "try:\n",
        "    import geemap\n",
        "except ImportError:\n",
        "    print('geemap package not installed. Installing ...')\n",
        "    subprocess.check_call([\"python\", '-m', 'pip', 'install', 'geemap'])\n",
        "\n",
        "# Checks whether this notebook is running on Google Colab\n",
        "try:\n",
        "    import google.colab\n",
        "    import geemap.eefolium as geemap\n",
        "except:\n",
        "    import geemap\n",
        "\n",
        "# Authenticates and initializes Earth Engine\n",
        "import ee\n",
        "\n",
        "try:\n",
        "    ee.Initialize()\n",
        "except Exception as e:\n",
        "    ee.Authenticate()\n",
        "    ee.Initialize()  "
      ],
      "outputs": [],
      "execution_count": null
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Create an interactive map \n",
        "The default basemap is `Google MapS`. [Additional basemaps](https://github.com/giswqs/geemap/blob/master/geemap/basemaps.py) can be added using the `Map.add_basemap()` function. "
      ]
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": [
        "Map = geemap.Map(center=[40,-100], zoom=4)\n",
        "Map"
      ],
      "outputs": [],
      "execution_count": null
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Add Earth Engine Python script "
      ]
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": [
        "# Add Earth Engine dataset\n",
        "# This demonstration uses hand-located points to train a classifier.\n",
        "# Each training point has a field called 'landcover' containing\n",
        "# class labels at that location. The following block contains\n",
        "# construction code for the points.  Hover on the 'urban' variable\n",
        "# and click, 'Convert' in the dialog.\n",
        "urban = ee.FeatureCollection(\n",
        "        [ee.Feature(\n",
        "            ee.Geometry.Point([-122.40898132324219, 37.78247386188714]),\n",
        "            {\n",
        "              \"landcover\": 0,\n",
        "              \"system:index\": \"0\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.40623474121094, 37.77107659627034]),\n",
        "            {\n",
        "              \"landcover\": 0,\n",
        "              \"system:index\": \"1\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.39799499511719, 37.785187237567705]),\n",
        "            {\n",
        "              \"landcover\": 0,\n",
        "              \"system:index\": \"2\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.39936828613281, 37.772162125840445]),\n",
        "            {\n",
        "              \"landcover\": 0,\n",
        "              \"system:index\": \"3\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.41104125976562, 37.76890548932033]),\n",
        "            {\n",
        "              \"landcover\": 0,\n",
        "              \"system:index\": \"4\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.41859436035156, 37.7835592241132]),\n",
        "            {\n",
        "              \"landcover\": 0,\n",
        "              \"system:index\": \"5\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.41790771484375, 37.801465399617314]),\n",
        "            {\n",
        "              \"landcover\": 0,\n",
        "              \"system:index\": \"6\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.40142822265625, 37.77053382550901]),\n",
        "            {\n",
        "              \"landcover\": 0,\n",
        "              \"system:index\": \"7\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.39662170410156, 37.75370595587201]),\n",
        "            {\n",
        "              \"landcover\": 0,\n",
        "              \"system:index\": \"8\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.28950500488281, 37.8166551148543]),\n",
        "            {\n",
        "              \"landcover\": 0,\n",
        "              \"system:index\": \"9\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.28195190429688, 37.82696064199382]),\n",
        "            {\n",
        "              \"landcover\": 0,\n",
        "              \"system:index\": \"10\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.28126525878906, 37.81882481909333]),\n",
        "            {\n",
        "              \"landcover\": 0,\n",
        "              \"system:index\": \"11\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.2723388671875, 37.82858769894982]),\n",
        "            {\n",
        "              \"landcover\": 0,\n",
        "              \"system:index\": \"12\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.28607177734375, 37.84702517033112]),\n",
        "            {\n",
        "              \"landcover\": 0,\n",
        "              \"system:index\": \"13\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.29293823242188, 37.8562421777618]),\n",
        "            {\n",
        "              \"landcover\": 0,\n",
        "              \"system:index\": \"14\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.27302551269531, 37.849193981623955]),\n",
        "            {\n",
        "              \"landcover\": 0,\n",
        "              \"system:index\": \"15\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.28057861328125, 37.86545803289311]),\n",
        "            {\n",
        "              \"landcover\": 0,\n",
        "              \"system:index\": \"16\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.27096557617188, 37.820452055421086]),\n",
        "            {\n",
        "              \"landcover\": 0,\n",
        "              \"system:index\": \"17\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.27920532226562, 37.808518155993234]),\n",
        "            {\n",
        "              \"landcover\": 0,\n",
        "              \"system:index\": \"18\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.27783203125, 37.80092285199884]),\n",
        "            {\n",
        "              \"landcover\": 0,\n",
        "              \"system:index\": \"19\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.31491088867188, 37.784644570400836]),\n",
        "            {\n",
        "              \"landcover\": 0,\n",
        "              \"system:index\": \"20\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.31903076171875, 37.7835592241132]),\n",
        "            {\n",
        "              \"landcover\": 0,\n",
        "              \"system:index\": \"21\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.26959228515625, 37.80200794325057]),\n",
        "            {\n",
        "              \"landcover\": 0,\n",
        "              \"system:index\": \"22\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.40966796875, 37.768362702622596]),\n",
        "            {\n",
        "              \"landcover\": 0,\n",
        "              \"system:index\": \"23\"\n",
        "            })]),\n",
        "vegetation = ee.FeatureCollection(\n",
        "        [ee.Feature(\n",
        "            ee.Geometry.Point([-122.15835571289062, 37.81990964729775]),\n",
        "            {\n",
        "              \"landcover\": 1,\n",
        "              \"system:index\": \"0\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.14462280273438, 37.806890656610484]),\n",
        "            {\n",
        "              \"landcover\": 1,\n",
        "              \"system:index\": \"1\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.16522216796875, 37.817197546892785]),\n",
        "            {\n",
        "              \"landcover\": 1,\n",
        "              \"system:index\": \"2\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.09793090820312, 37.80797566018445]),\n",
        "            {\n",
        "              \"landcover\": 1,\n",
        "              \"system:index\": \"3\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.08282470703125, 37.81123057525427]),\n",
        "            {\n",
        "              \"landcover\": 1,\n",
        "              \"system:index\": \"4\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.07733154296875, 37.7992951852321]),\n",
        "            {\n",
        "              \"landcover\": 1,\n",
        "              \"system:index\": \"5\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.05398559570312, 37.77867496858311]),\n",
        "            {\n",
        "              \"landcover\": 1,\n",
        "              \"system:index\": \"6\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.05398559570312, 37.76673431862507]),\n",
        "            {\n",
        "              \"landcover\": 1,\n",
        "              \"system:index\": \"7\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.07389831542969, 37.792784159505125]),\n",
        "            {\n",
        "              \"landcover\": 1,\n",
        "              \"system:index\": \"8\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.0306396484375, 37.83455326751277]),\n",
        "            {\n",
        "              \"landcover\": 1,\n",
        "              \"system:index\": \"9\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.06497192382812, 37.831299380818606]),\n",
        "            {\n",
        "              \"landcover\": 1,\n",
        "              \"system:index\": \"10\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.19268798828125, 37.85461573076714]),\n",
        "            {\n",
        "              \"landcover\": 1,\n",
        "              \"system:index\": \"11\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.18170166015625, 37.849193981623955]),\n",
        "            {\n",
        "              \"landcover\": 1,\n",
        "              \"system:index\": \"12\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.51609802246094, 37.84051835371829]),\n",
        "            {\n",
        "              \"landcover\": 1,\n",
        "              \"system:index\": \"13\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.49137878417969, 37.838349287273296]),\n",
        "            {\n",
        "              \"landcover\": 1,\n",
        "              \"system:index\": \"14\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.50511169433594, 37.82641828170282]),\n",
        "            {\n",
        "              \"landcover\": 1,\n",
        "              \"system:index\": \"15\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.54081726074219, 37.84160286302103]),\n",
        "            {\n",
        "              \"landcover\": 1,\n",
        "              \"system:index\": \"16\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.55592346191406, 37.85353141283498]),\n",
        "            {\n",
        "              \"landcover\": 1,\n",
        "              \"system:index\": \"17\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.56278991699219, 37.86274760688767]),\n",
        "            {\n",
        "              \"landcover\": 1,\n",
        "              \"system:index\": \"18\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.54631042480469, 37.86328970006369]),\n",
        "            {\n",
        "              \"landcover\": 1,\n",
        "              \"system:index\": \"19\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.52708435058594, 37.85190490603355]),\n",
        "            {\n",
        "              \"landcover\": 1,\n",
        "              \"system:index\": \"20\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.52845764160156, 37.83889155986444]),\n",
        "            {\n",
        "              \"landcover\": 1,\n",
        "              \"system:index\": \"21\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.51472473144531, 37.83021472002989]),\n",
        "            {\n",
        "              \"landcover\": 1,\n",
        "              \"system:index\": \"22\"\n",
        "            })]),\n",
        "water = ee.FeatureCollection(\n",
        "        [ee.Feature(\n",
        "            ee.Geometry.Point([-122.61085510253906, 37.835095568009415]),\n",
        "            {\n",
        "              \"landcover\": 2,\n",
        "              \"system:index\": \"0\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.60673522949219, 37.8166551148543]),\n",
        "            {\n",
        "              \"landcover\": 2,\n",
        "              \"system:index\": \"1\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.60810852050781, 37.80038030039511]),\n",
        "            {\n",
        "              \"landcover\": 2,\n",
        "              \"system:index\": \"2\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.57102966308594, 37.80472060163741]),\n",
        "            {\n",
        "              \"landcover\": 2,\n",
        "              \"system:index\": \"3\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.58888244628906, 37.83455326751277]),\n",
        "            {\n",
        "              \"landcover\": 2,\n",
        "              \"system:index\": \"4\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.65205383300781, 37.855157883752504]),\n",
        "            {\n",
        "              \"landcover\": 2,\n",
        "              \"system:index\": \"5\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.62870788574219, 37.823164036248635]),\n",
        "            {\n",
        "              \"landcover\": 2,\n",
        "              \"system:index\": \"6\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.62664794921875, 37.792784159505125]),\n",
        "            {\n",
        "              \"landcover\": 2,\n",
        "              \"system:index\": \"7\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.60055541992188, 37.792784159505125]),\n",
        "            {\n",
        "              \"landcover\": 2,\n",
        "              \"system:index\": \"8\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.53738403320312, 37.7992951852321]),\n",
        "            {\n",
        "              \"landcover\": 2,\n",
        "              \"system:index\": \"9\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.57240295410156, 37.82641828170282]),\n",
        "            {\n",
        "              \"landcover\": 2,\n",
        "              \"system:index\": \"10\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.58682250976562, 37.823164036248635]),\n",
        "            {\n",
        "              \"landcover\": 2,\n",
        "              \"system:index\": \"11\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.36709594726562, 37.85570003275074]),\n",
        "            {\n",
        "              \"landcover\": 2,\n",
        "              \"system:index\": \"12\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.40074157714844, 37.88171849539308]),\n",
        "            {\n",
        "              \"landcover\": 2,\n",
        "              \"system:index\": \"13\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.40005493164062, 37.86925246182428]),\n",
        "            {\n",
        "              \"landcover\": 2,\n",
        "              \"system:index\": \"14\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.37739562988281, 37.88117653780091]),\n",
        "            {\n",
        "              \"landcover\": 2,\n",
        "              \"system:index\": \"15\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.34992980957031, 37.87358871277159]),\n",
        "            {\n",
        "              \"landcover\": 2,\n",
        "              \"system:index\": \"16\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.34992980957031, 37.85244707895444]),\n",
        "            {\n",
        "              \"landcover\": 2,\n",
        "              \"system:index\": \"17\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.34855651855469, 37.838349287273296]),\n",
        "            {\n",
        "              \"landcover\": 2,\n",
        "              \"system:index\": \"18\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.39387512207031, 37.849193981623955]),\n",
        "            {\n",
        "              \"landcover\": 2,\n",
        "              \"system:index\": \"19\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.44743347167969, 37.82262164805511]),\n",
        "            {\n",
        "              \"landcover\": 2,\n",
        "              \"system:index\": \"20\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.43850708007812, 37.842687356377084]),\n",
        "            {\n",
        "              \"landcover\": 2,\n",
        "              \"system:index\": \"21\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.354736328125, 37.789528431453014]),\n",
        "            {\n",
        "              \"landcover\": 2,\n",
        "              \"system:index\": \"22\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.25826263427734, 37.68952589794135]),\n",
        "            {\n",
        "              \"landcover\": 2,\n",
        "              \"system:index\": \"23\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.53326416015625, 37.81114015184751]),\n",
        "            {\n",
        "              \"landcover\": 2,\n",
        "              \"system:index\": \"24\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.44503021240234, 37.87078823552829]),\n",
        "            {\n",
        "              \"landcover\": 2,\n",
        "              \"system:index\": \"25\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.4532699584961, 37.86048883137166]),\n",
        "            {\n",
        "              \"landcover\": 2,\n",
        "              \"system:index\": \"26\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.33036041259766, 37.833920567528345]),\n",
        "            {\n",
        "              \"landcover\": 2,\n",
        "              \"system:index\": \"27\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.19989776611328, 37.65664491891396]),\n",
        "            {\n",
        "              \"landcover\": 2,\n",
        "              \"system:index\": \"28\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.21946716308594, 37.62374937200642]),\n",
        "            {\n",
        "              \"landcover\": 2,\n",
        "              \"system:index\": \"29\"\n",
        "            }),\n",
        "        ee.Feature(\n",
        "            ee.Geometry.Point([-122.24040985107422, 37.61504728801728]),\n",
        "            {\n",
        "              \"landcover\": 2,\n",
        "              \"system:index\": \"30\"\n",
        "            })])\n",
        "\n",
        "# Load the Landsat 8 scaled radiance image collection.\n",
        "landsatCollection = ee.ImageCollection('LANDSAT/LC08/C01/T1') \\\n",
        "    .filterDate('2017-01-01', '2017-12-31')\n",
        "\n",
        "# Make a cloud-free composite.\n",
        "composite = ee.Algorithms.Landsat.simpleComposite({\n",
        "  'collection': landsatCollection,\n",
        "  'asFloat': True\n",
        "})\n",
        "\n",
        "# Merge the three geometry layers into a single FeatureCollection.\n",
        "newfc = urban.merge(vegetation).merge(water)\n",
        "\n",
        "# Use these bands for classification.\n",
        "bands = ['B2', 'B3', 'B4', 'B5', 'B6', 'B7']\n",
        "# The name of the property on the points storing the class label.\n",
        "classProperty = 'landcover'\n",
        "\n",
        "# Sample the composite to generate training data.  Note that the\n",
        "# class label is stored in the 'landcover' property.\n",
        "training = composite.select(bands).sampleRegions({\n",
        "  'collection': newfc,\n",
        "  'properties': [classProperty],\n",
        "  'scale': 30\n",
        "})\n",
        "\n",
        "# Train a CART classifier.\n",
        "classifier = ee.Classifier.smileCart().train({\n",
        "  'features': training,\n",
        "  'classProperty': classProperty,\n",
        "})\n",
        "# Print some info about the classifier (specific to CART).\n",
        "print('CART, explained', classifier.explain())\n",
        "\n",
        "# Classify the composite.\n",
        "classified = composite.classify(classifier)\n",
        "Map.centerObject(newfc)\n",
        "Map.addLayer(classified, {'min': 0, 'max': 2, 'palette': ['red', 'green', 'blue']})\n",
        "\n",
        "# Optionally, do some accuracy assessment.  Fist, add a column of\n",
        "# random uniforms to the training dataset.\n",
        "withRandom = training.randomColumn('random')\n",
        "\n",
        "# We want to reserve some of the data for testing, to avoid overfitting the model.\n",
        "split = 0.7;  # Roughly 70% training, 30% testing.\n",
        "trainingPartition = withRandom.filter(ee.Filter.lt('random', split))\n",
        "testingPartition = withRandom.filter(ee.Filter.gte('random', split))\n",
        "\n",
        "# Trained with 70% of our data.\n",
        "trainedClassifier = ee.Classifier.smileRandomForest(5).train({\n",
        "  'features': trainingPartition,\n",
        "  'classProperty': classProperty,\n",
        "  'inputProperties': bands\n",
        "})\n",
        "\n",
        "# Classify the test FeatureCollection.\n",
        "test = testingPartition.classify(trainedClassifier)\n",
        "\n",
        "# Print the confusion matrix.\n",
        "confusionMatrix = test.errorMatrix(classProperty, 'classification')\n",
        "print('Confusion Matrix', confusionMatrix)\n"
      ],
      "outputs": [],
      "execution_count": null
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Display Earth Engine data layers "
      ]
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": [
        "Map.addLayerControl() # This line is not needed for ipyleaflet-based Map.\n",
        "Map"
      ],
      "outputs": [],
      "execution_count": null
    }
  ],
  "metadata": {
    "anaconda-cloud": {},
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.6.1"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 1
}